/*
 *  Copyright (C) 2010 Glauco
 * 
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 * 
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 * 
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.balisunrise.swing.transaction;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.util.HashMap;
import java.util.Map;
import javax.swing.BorderFactory;
import javax.swing.GroupLayout;
import javax.swing.Icon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.LayoutStyle;
import javax.swing.SwingConstants;
import org.balisunrise.common.BaliIcons;
import org.balisunrise.common.TransactionMode;
import org.balisunrise.common.VisualEntity;
import org.balisunrise.common.VisualEntityType;
import org.balisunrise.common.abstracts.AbstractVisualEntity;

/**
 *
 * @author Glauco
 */
public class Messenger extends AbstractVisualEntity
        implements org.balisunrise.common.Messenger<JPanel>{

    private JPanel visualRepresentation;

    private String mode;

    private javax.swing.JLabel lbMode;

    private Map<String,JLabel> messages;

    public Messenger(Class entityClass, String alias) {
        this.entityClass = entityClass;
        this.alias = alias;
        this.messages = new HashMap<String, JLabel>();
        initComponents();
    }

    public void setVisualRepresentation(JPanel visualRepresentation) {
        this.visualRepresentation = visualRepresentation;
    }

    public void setMode(int mode) {
        switch(mode){
            case TransactionMode.NEW:
                this.mode = "Inserindo";
                lbMode.setIcon(BaliIcons.getIcon(BaliIcons.INSERT));
                break;
            case TransactionMode.UPDATE:
                this.mode = "Editando";
                lbMode.setIcon(BaliIcons.getIcon(BaliIcons.UPDATE));
                break;
            case TransactionMode.DELETE:
                this.mode = "Excluir?";
                lbMode.setIcon(BaliIcons.getIcon(BaliIcons.DELETE));
                break;
            default:
                this.mode = new String();
                lbMode.setIcon(null);
        }
        lbMode.setText(this.mode);
    }

    public void addMessage(String id, String message, int level) {
        
        Icon icon = null;
        
        switch (level) {
            case 0:
                icon = BaliIcons.getIcon(BaliIcons.CONFIRM1);
                break;
            case 1:
                icon = BaliIcons.getIcon(BaliIcons.INFORMATION);
                break;
            case 2:
                icon = BaliIcons.getIcon(BaliIcons.WARNING);
                break;
            case 3:
                icon = BaliIcons.getIcon(BaliIcons.ERROR);
                break;
        }

        JLabel msg = new JLabel(message, icon, SwingConstants.LEFT);

        String name = entityClass.getName() + "_" + alias + "_message_" + id;
        msg.setName(name);
        msg.getAccessibleContext().setAccessibleName(name);
        msg.getAccessibleContext().setAccessibleName(String.valueOf(level));

        //System.out.println("Message[" + id + "]: " + message);

        messages.put(id, msg);
        makeLayout();
    }

    public void removeMessage(String id) {
        messages.remove(id);
        makeLayout();
    }

    public void removeAllMessages() {
        messages.clear();
        makeLayout();
    }

    public JPanel getVisualRepresentation() {
        return visualRepresentation;
    }

    public boolean hasError() {

        if(messages.isEmpty())
            return false;

        for (JLabel msg : messages.values()) {
            int level = Integer.parseInt(
                    msg.getAccessibleContext().getAccessibleDescription());
            if(level == 3)
                return true;
        }
        
        return false;
    }

    /**
     * Retorna o tipo do VisualEntity.<BR>
     * Sempre retornará MESSENGER.
     * @return VisualEntityType.MESSENGER
     */
    public VisualEntityType getVisualEntityType() {
        return VisualEntityType.MESSENGER;
    }

    private void initComponents() {

        lbMode = new JLabel();

        visualRepresentation = new JPanel();
        visualRepresentation.setBackground(new Color(255, 255, 255));
        visualRepresentation.setBorder(BorderFactory.createLineBorder(new Color(240, 240, 240), 3));
        visualRepresentation.setMaximumSize(new java.awt.Dimension(32767, 500));
        Dimension size = new Dimension(0, 75);
        visualRepresentation.setPreferredSize(size);
        visualRepresentation.setMinimumSize(size);

        lbMode.setFont(new Font("Tahoma", Font.BOLD, 11));
        lbMode.setIcon(BaliIcons.getIcon(BaliIcons.INSERT));
        lbMode.setText("Inserindo");
        String lbModName = entityClass.getName() + "_messageModeLabel_" + alias;
        lbMode.setName(lbModName);

        makeLayout();
    }

    private void makeLayout(){

        visualRepresentation.removeAll();
        GroupLayout layout = new GroupLayout(visualRepresentation);
        visualRepresentation.setLayout(layout);

        // HORIZONTAL
        GroupLayout.ParallelGroup hGroup =
                layout.createParallelGroup(GroupLayout.Alignment.LEADING, true);

        // VERTICAL
        GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();

        hGroup.addGroup(layout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(lbMode)
                    .addContainerGap());

        vGroup.addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(lbMode)
                .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED));

        for (JLabel msg : messages.values()) {
            hGroup.addComponent(msg);
            vGroup.addComponent(msg);
        }

        layout.setHorizontalGroup(layout.createSequentialGroup()
                .addGroup(hGroup));

        layout.setVerticalGroup(
            layout.createParallelGroup(GroupLayout.Alignment.LEADING, true)
            .addGroup(vGroup));
    }

    public void setOwner(VisualEntity owner) {
        super.setOwner(owner);
    }

    public String getName() {
        return getVisualRepresentation().getName();
    }
}
